<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>EASY后台-导入</title>
    <link rel="icon" th:href="@{/static/images/favicon.ico}">
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
</head>
<body>
<div class="easy-container">
    <form class="layui-form" id="dataForm" lay-filter="dataForm">
        <div class="layui-form-item">
            <label class="layui-form-label">表名</label>
            <div class="layui-input-inline">
                <input type="text" name="tableName" placeholder="请输入" class="layui-input">
            </div>
            <button class="layui-btn layui-btn-md" lay-submit lay-filter="tableSearch">
                <i class="layui-icon layui-icon-search"></i>
                查询
            </button>
        </div>
        <div class="layui-form-item">
            <table class="layui-hide" id="tableTable" lay-filter="tableTable"></table>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submitForm">导入</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="cancelForm">取消</button>
            </div>
        </div>
    </form>
</div>
</body>

<script th:src="@{/static/js/jquery.js}"></script>
<script th:src="@{/static/layui/layui.js}"></script>
<script type="text/javascript" th:inline="javascript">
    layui.use(['element', 'layer', 'table', 'form'], function () {
        var element = layui.element;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;

        // table渲染
        var tableObj = table.render({
            elem: '#tableTable',
            url: '/genTable/pageDbTable',
            method: 'GET',
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            where: { // 添加额外条件

            },
            cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {type: 'numbers', title: '序号', align: 'center'},
                    {field: 'tableName', title: '表名', width: 200, align: 'center'},
                    {field: 'tableComment', title: '表注释', align: 'center'}
                ]
            ],
            page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'], // 自定义分页布局
                limit: 10,
                limits: [5, 10, 20, 50, 100],
                curr: 1,
                groups: 5, // 只显示5个连续页码
                first: false,
                last: false
            },
            loading: true,
            skin: 'grid', // 边框风格
            size: 'md', // 尺寸
            even: true, // 隔行背景
            parseData: function (res) { // 数据格式解析的回调函数
                return {
                    "code": res.code === 200 ? 0 : res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.results
                }
            },
        });

        // 监听查询
        form.on('submit(tableSearch)', function (data) {
            tableObj.reload({page: {curr: 1}, where: data.field});
            return false;
        });

        // 保存
        form.on('submit(submitForm)', function (form) {
            var checkRows = table.checkStatus('tableTable');
            if (checkRows.data.length === 0) {
                layer.msg('请先选择数据', {icon: 0});
                return;
            }
            layer.confirm('确定导入所选数据吗？', {
                btn: ['确定', '关闭']
            }, function () {
                var ids = checkRows.data.map(function (row) {
                    return row.tableName;
                });
                $.ajax({
                    url: '/genTable/import',
                    type: 'POST',
                    contentType: 'application/json',
                    dataType: 'json',
                    data: JSON.stringify({tables: ids}),
                    success: function (response) {
                        if (response.code === 200) {
                            // 关闭
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            // 刷新
                            parent.location.reload();

                            layer.msg('导入成功', {icon: 1});
                        } else {
                            layer.msg(response.msg, {icon: 0});
                        }
                    },
                    error: function (xhr, status, error) {
                        layer.msg('请求失败', {icon: 0});
                    }
                });
            });
            return false;
        });

        // 取消
        $('#cancelForm').on('click', function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
            return false;
        });

    });
</script>
</html>